package cn.edu.wynu.array;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        // 冒泡排序,随机数组
        int[] ints = {36, 27, 20, 60, 55, 7, 28, 36, 52, 44, 16};
        // 对于长度为n的数组,需要遍历n-1轮
        System.out.println("第1轮第0次排序: " + Arrays.toString(ints));

        for(int i = 0; i < ints.length - 1; i++) {
            for (int j =0; j < ints.length - 1 - i; j++) {
                // 如果前一个元素大于后一个元素,则交换位置
                if (ints[j] > ints[j + 1]) {
                    int tmp = ints[j];
                    ints[j] = ints[j + 1];
                    ints[j + 1] = tmp;
                }
                System.out.println("第" + (i + 1) + "轮第" + (j + 1) + "次排序: " + Arrays.toString(ints));
            }
        }

        System.out.println("排序后: " + Arrays.toString(ints));
    }
}
